// 悬浮窗口逻辑
let isDragging = false;
let currentX;
let currentY;
let initialX;
let initialY;
let xOffset = 0;
let yOffset = 0;

const floatingApp = document.getElementById('floating-app');
const dragHandle = document.getElementById('drag-handle');

// 拖拽功能
dragHandle.addEventListener('mousedown', dragStart);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', dragEnd);

// 触摸设备支持
dragHandle.addEventListener('touchstart', dragStart);
document.addEventListener('touchmove', drag);
document.addEventListener('touchend', dragEnd);

function dragStart(e) {
    if (e.type === "touchstart") {
        initialX = e.touches[0].clientX - xOffset;
        initialY = e.touches[0].clientY - yOffset;
    } else {
        initialX = e.clientX - xOffset;
        initialY = e.clientY - yOffset;
    }

    if (e.target === dragHandle || dragHandle.contains(e.target)) {
        isDragging = true;
    }
}

function drag(e) {
    if (isDragging) {
        e.preventDefault();
        
        if (e.type === "touchmove") {
            currentX = e.touches[0].clientX - initialX;
            currentY = e.touches[0].clientY - initialY;
        } else {
            currentX = e.clientX - initialX;
            currentY = e.clientY - initialY;
        }

        xOffset = currentX;
        yOffset = currentY;

        setTranslate(currentX, currentY, floatingApp);
    }
}

function setTranslate(xPos, yPos, el) {
    el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
}

function dragEnd(e) {
    initialX = currentX;
    initialY = currentY;
    isDragging = false;
}

// 窗口控制
document.getElementById('close-btn').addEventListener('click', () => {
    // 发送消息给background关闭窗口
    chrome.runtime.sendMessage({ action: 'closeFloatingWindow' });
});

document.getElementById('minimize-btn').addEventListener('click', () => {
    // 最小化逻辑（可以隐藏窗口或缩小）
    floatingApp.style.display = floatingApp.style.display === 'none' ? 'block' : 'none';
});

// 加载任务数据
function loadTasks() {
    chrome.runtime.sendMessage({ action: 'getTasks' }, (response) => {
        const content = document.getElementById('floating-content');
        const tasks = response || [];
        
        if (tasks.length === 0) {
            content.innerHTML = `
                <div class="empty-state">
                    <div class="empty-icon">📋</div>
                    <div>暂无任务</div>
                </div>
            `;
            return;
        }
        
        const tasksHtml = tasks.map(task => `
            <div class="task-item">
                <div class="task-title">${task.title || '未命名任务'}</div>
                <div class="task-status">${getStatusText(task.status)}</div>
            </div>
        `).join('');
        
        content.innerHTML = tasksHtml;
    });
}

function getStatusText(status) {
    const statusMap = {
        'pending': '等待中',
        'running': '同步中',
        'success': '同步成功',
        'failed': '同步失败'
    };
    return statusMap[status] || '未知状态';
}

// 初始化
loadTasks();

// 定期更新任务状态
setInterval(loadTasks, 5000);

// 监听来自background的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.action === 'updateTasks') {
        loadTasks();
    }
});